#!/usr/bin/perl

# ---------- Init variables -------
#my $version = "0.0001";
# ---------- external readable parameters -------
## Functions
##-------------------------------------------------------
#
# stict syntax checking.
use strict;
#use SIM_base;
#use SIM_settings;
use Net::Flow qw(decode) ;
use IO::Socket::INET;

my $receive_port = 9993 ;
my $packet = undef ;
my $TemplateArrayRef = undef ;
my $sock = IO::Socket::INET->new( LocalPort =>$receive_port, Proto => 'udp') ;

#my $baseDir = $SIMparam{'SIMtrafficAnalyserDir'};
my $baseDir = "test";
print("\n<B>Listening for netflow info.</B>
       baseDir		= $baseDir
       receive_port	= $receive_port
       ");
sub make_hex_string {
   my $str = unpack "H*", shift @_;
   $str =~ s/(..)/:$1/g;
   $str =~ s/^://;
   $str;
}

# ========================== Main =============================
while ($sock->recv($packet,1548)) {
  my $ip = $sock->peerhost ;

        my ($HeaderHashRef,$FlowArrayRef,$ErrorsArrayRef)=() ;

          ( $HeaderHashRef,
            $TemplateArrayRef,
            $FlowArrayRef,
            $ErrorsArrayRef)
            = Net::Flow::decode(
                                \$packet,
                                $TemplateArrayRef
                                ) ;
    my $header = substr($packet,0,90);
    my $hexInfo = make_hex_string($header);
    print "\n-- Sender: $ip, $hexInfo --\n" ;


        grep{ print "$_\n" }@{$ErrorsArrayRef} if( @{$ErrorsArrayRef} ) ;

        print "\n- Header Information -\n" ;
        foreach my $Key ( sort keys %{$HeaderHashRef} ){
            printf " %s = %3d\n",$Key,$HeaderHashRef->{$Key} ;
        }

        foreach my $TemplateRef ( @{$TemplateArrayRef} ){
            print "\n-- Template Information --\n" ;

            foreach my $TempKey ( sort keys %{$TemplateRef} ){
                if( $TempKey eq "Template" ){
                    printf "  %s = \n",$TempKey ;
                    foreach my $Ref ( @{$TemplateRef->{Template}}  ){
                        foreach my $Key ( keys %{$Ref} ){
                            printf "   %s=%s", $Key, $Ref->{$Key} ;
                        }
                        print "\n" ;
                    }
                }else{
                    printf "  %s = %s\n", $TempKey, $TemplateRef->{$TempKey} ;
                }
            }
        }

        foreach my $FlowRef ( @{$FlowArrayRef} ){
            print "\n-- Flow Information --\n" ;

            foreach my $Id ( sort keys %{$FlowRef} ){
               if( $Id eq "SetId" ){
                   print "  $Id=$FlowRef->{$Id}\n" if defined $FlowRef->{$Id} ;
               }elsif( ref $FlowRef->{$Id} ){
                   printf "  Id=%s Value=",$Id ;
                   foreach my $Value ( @{$FlowRef->{$Id}} ){
                      printf "%s,",unpack("H*",$Value) ;
                   }
                   print "\n" ;
               }else{
                   printf "  Id=%s Value=%s\n",$Id,unpack("H*",$FlowRef->{$Id}) ;
               }
	    }   
        }
    }
